﻿#include "FITKAbaqusHDF5AdaptorSection.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"

#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"

#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionSolidHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfile.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaProfileI.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignSolidHomogenous.h"

#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorSection::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorSection";
    }

    bool FITKAbaqusHDF5AdaptorSection::adaptR()
    {
        return FITKInterfaceHDF5AdaptorSection::adaptR();
    }

    bool FITKAbaqusHDF5AdaptorSection::adaptW()
    {
        return FITKInterfaceHDF5AdaptorSection::adaptW();
    }

    bool FITKAbaqusHDF5AdaptorSection::readSection(Interface::FITKAbstractSection * section, H5::Group & h5Group)
    {
        auto abaqusSection = dynamic_cast<Interface::FITKAbaAbastractSection*>(section);
        if (!_reader || !_h5Group || !abaqusSection) return false;
        bool isR = true;
        isR &= FITKInterfaceHDF5AdaptorSection::readSection(section, h5Group);
        //获取类型 写出
        Interface::FITKAbaAbastractSection::FITKAbaSectionType type = abaqusSection->getSectionType();
        if (type == Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam)
        {
            isR &= readBeamSection(abaqusSection, h5Group);
        }
        else if (type == Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous)
        {
            isR &= readShellHomogeneousSection(abaqusSection, h5Group);

        }
        else if (type == Interface::FITKAbaAbastractSection::FITKAbaSectionType::SolidHomogeneous)
        {
            isR &= readSolidHomogeneousSection(abaqusSection, h5Group);

        }
        else if (type == Interface::FITKAbaAbastractSection::FITKAbaSectionType::Truss)
        {
            return false;
        }

        return isR;
    }

    bool FITKAbaqusHDF5AdaptorSection::readSolidHomogeneousSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionSolidHomogeneous* solidSection = dynamic_cast<Interface::FITKAbaSectionSolidHomogeneous*>(section);
        if (!solidSection) return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSection::readShellHomogeneousSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionShellHomogeneous* shellection = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(section);
        if (!shellection) return false;
        double Thickness = readDoubleAttribute(h5Group, "Thickness");
        shellection->setThickness(Thickness);
        int ThicknessIntergrationPoints = readIntAttribute(h5Group, "ThicknessIntergrationPoints");
        shellection->setThicknessIntergrationPoints(ThicknessIntergrationPoints);
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSection::readBeamSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionBeam* beamSection = dynamic_cast<Interface::FITKAbaSectionBeam*>(section);
        if (!beamSection) return false;
        bool isValid = false;
        int profileID = -1;
        profileID = readIntAttribute(h5Group, "ProfileID");
        beamSection->setProfileId(profileID);

        //写出类型
        auto sTemperatureVariation = readStrAttribute(h5Group, "TemperatureVariation");
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKAbaAbastractSection::TemperatureVariation> fitkTypeTrafer;
        auto eTemperatureVariation = fitkTypeTrafer.fromString(QString::fromStdString(sTemperatureVariation), isValid);
        if (!isValid) return false;
        beamSection->setTemperatureVariation(eTemperatureVariation);
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSection::writeSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        auto abaqusSection = dynamic_cast<Interface::FITKAbaAbastractSection*>(section);
        if (!_writer || !_h5Group || !abaqusSection) return false;
        bool isW = true;
        bool isValid = false;

        isW &= FITKInterfaceHDF5AdaptorSection::writeSection(abaqusSection, h5Group);
        if (_h5CreateGroup.getLocId() == -1) return false;

        //写出类型
        auto eType = abaqusSection->getSectionType();
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKAbaAbastractSection::FITKAbaSectionType> fitkTypeTrafer;
        //写出类型
        QString TypeStr = fitkTypeTrafer.toStrting(eType, isValid);
        if (!isValid) return false;
        writeStrAttribute(_h5CreateGroup, "Type", TypeStr.toStdString());

        //获取类型 写出
        if (eType == Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam)
        {
            isW &= writeBeamSection(abaqusSection, _h5CreateGroup);
        }
        else if (eType == Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous)
        {
            isW &= writeShellHomogeneousSection(abaqusSection, _h5CreateGroup);
        }
        else if (eType == Interface::FITKAbaAbastractSection::FITKAbaSectionType::SolidHomogeneous)
        {
            isW &= writeSolidHomogeneousSection(abaqusSection, _h5CreateGroup);
        }
        else if (eType == Interface::FITKAbaAbastractSection::FITKAbaSectionType::Truss)
        {
            isW = false;
        }
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorSection::writeBeamSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionBeam* beamSection = dynamic_cast<Interface::FITKAbaSectionBeam*>(section);
        if (!beamSection) return false;
        bool isValid = false;
        int profileID = beamSection->getProfileId();
        writeIntAttribute(h5Group, "ProfileID", &profileID);

        //写出类型
        auto eTemperatureVariation = beamSection->getTemperatureVariation();
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKAbaAbastractSection::TemperatureVariation> fitkTypeTrafer;
        QString sTemperatureVariation = fitkTypeTrafer.toStrting(eTemperatureVariation, isValid);
        if (!isValid) return false;
        writeStrAttribute(h5Group, "TemperatureVariation", sTemperatureVariation.toStdString());

        return true;
    }

    bool FITKAbaqusHDF5AdaptorSection::writeShellHomogeneousSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionShellHomogeneous* shellection = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(section);
        if (!shellection) return false;
        double Thickness = shellection->getThickness();
        writeDoubleAttribute(h5Group, "Thickness", &Thickness);
        int ThicknessIntergrationPoints = shellection->getThicknessIntergrationPoints();
        writeIntAttribute(h5Group, "ThicknessIntergrationPoints", &ThicknessIntergrationPoints);
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSection::writeSolidHomogeneousSection(Interface::FITKAbstractSection* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionSolidHomogeneous* solidection = dynamic_cast<Interface::FITKAbaSectionSolidHomogeneous*>(section);
        if (!solidection)return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSection::writeBeamSectionProfileI(Interface::FITKAbaProfile* profile, H5::Group& h5Group)
    {
        Interface::FITKAbaProfileI* profileI = dynamic_cast<Interface::FITKAbaProfileI*>(profile);
        if (!profileI) return false;
        double l = profileI->getI();
        double h = profileI->geth();
        double b1 = profileI->getb1();
        double b2 = profileI->getb2();
        double t1 = profileI->gett1();
        double t2 = profileI->gett2();
        double t3 = profileI->gett3();
        writeDoubleAttribute(h5Group, "l", &l);
        writeDoubleAttribute(h5Group, "h", &h);
        writeDoubleAttribute(h5Group, "b1", &b1);
        writeDoubleAttribute(h5Group, "b2", &b2);
        writeDoubleAttribute(h5Group, "t1", &t1);
        writeDoubleAttribute(h5Group, "t2", &t2);
        writeDoubleAttribute(h5Group, "t3", &t3);
        return true;
    }
}


